//
//  CreateShareViewController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/12/18.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "CreateShareViewController.h"
#import "ImageViewCell.h"
#import "HTextViewCell.h"
#import "ShareView.h"
#import "HQBrowerController.h"
#import "ShareGetOutView.h"
@interface CreateShareViewController ()<UITableViewDelegate, UITableViewDataSource, BrowerDelegate>
@property (nonatomic, strong) UITableView *shareTable;
@property (nonatomic, strong) ShareView *shareView;
@property (nonatomic, weak) ShareGetOutView *getoutView;
@end

@implementation CreateShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"创建分享";
    self.view.backgroundColor = WhiteColor;
    
    ShareGetOutView *getoutView = [[ShareGetOutView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-Height_NavBar)];
    [self.view addSubview:getoutView];
    getoutView.shareUrl = self.imageArry.count > 0 ? self.imageArry[0] : @"";
    getoutView.listModel = self.model;
    getoutView.shareQRImageUrl = self.item_url;
    self.getoutView = getoutView;
    
    [self.view addSubview:self.shareTable];

    self.shareView = [[ShareView alloc] init];
    self.shareView.vc = self;
    [self.view addSubview:self.shareView];
    [self.shareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(jisuan(300));
    }];
    
    WS(weakSelf);
    #pragma mark 点击分享回调
    self.shareView.shareImageBlock = ^(UMSocialPlatformType type) {
        [[KYAlertView sharedInstance] showLoadingViewWith];
        [weakSelf createShareImageType:type];
    };
}

- (void)createShareImageType:(UMSocialPlatformType)type {
    WS(weakSelf);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
      __block  UIImage *image;
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *imagea = [weakSelf GetmakeImageWithView:weakSelf.getoutView andWithSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-Height_NavBar)];
            image = imagea;
        });
        dispatch_async(dispatch_get_main_queue(), ^{
            [[KYAlertView sharedInstance] dimissLoadingView];
            UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
            UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
            [shareObject setShareImage:image];
            messageObject.shareObject = shareObject;
            [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:weakSelf completion:^(id data, NSError *error) {
            }];
        });
    });
    
}


- (UIImage *)GetmakeImageWithView:(UIView *)view andWithSize:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    WS(weakSelf);
    
    #pragma mark 点击图片放大回调
    ImageViewCell *imgCell = [tableView dequeueReusableCellWithIdentifier:@"ImageViewCell"];
    imgCell.imageArray = self.imageArry;
    imgCell.didSelectClickBlock = ^(NSInteger index) {
        [weakSelf tapAction:index];
    };
    
    #pragma mark 选择图片回调
    imgCell.buttonHSelectClickBlock = ^(NSInteger index) {
        weakSelf.getoutView.shareUrl = weakSelf.imageArry[index];
    };
    HTextViewCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"HTextViewCell"];
    [textCell setModel:self.model WithItemUrl:self.item_url];
    return indexPath.row == 0 ? imgCell : textCell;
}

- (void)tapAction:(NSInteger)index{
    [self presentViewController:({
        HQBrowerController * vc = [[HQBrowerController alloc] init];
        vc.delegate = self;
        vc.imageArr = self.imageArry;
        vc.tag = index;
        vc.originalRect = CGRectMake(0, 100, WIDTH, HEIGHT-200);
        vc;
    }) animated:NO completion:nil];
}

- (void)dismissViewWithTurnToTag:(NSInteger)tag{}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.row == 0 ? jisuan(316) : jisuan(500);
}

- (UITableView *)shareTable {
    if (!_shareTable) {
        _shareTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-20-jisuan(300))];
        _shareTable.backgroundColor = WhiteColor;
        _shareTable.delegate = self;
        _shareTable.dataSource = self;
        _shareTable.showsVerticalScrollIndicator = NO;
        _shareTable.showsHorizontalScrollIndicator = NO;
        _shareTable.tableFooterView = [UIView new];
        _shareTable.estimatedRowHeight = 0;
        _shareTable.estimatedSectionFooterHeight = 0;
        _shareTable.estimatedSectionHeaderHeight = 0;
        _shareTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_shareTable registerClass:[ImageViewCell class] forCellReuseIdentifier:@"ImageViewCell"];
        [_shareTable registerClass:[HTextViewCell class] forCellReuseIdentifier:@"HTextViewCell"];
    }
    return _shareTable;
}
@end
